﻿using Ext.Net.MVC;
using System;
using System.Collections.Generic;
using Ext.Net;
using Sharp.DataTransferObject;
using Sharp.Portal;
using Sharp.Portal.ExtNet;
using Sharp.Portal.Mvc;
using Sharp.Portal.Mvc.List;
using Sharp.Portal.Other;
using Alignment = Ext.Net.Alignment;
using Column = Ext.Net.MVC.ColumnAttribute;
using DateColumn = Ext.Net.MVC.DateColumnAttribute;
using BooleanColumn = Ext.Net.MVC.BooleanColumnAttribute;
 

namespace Sharp.WebBooter.Areas.Platform.Models
{
    [Title("用户管理")]
    [DataHandler(ModuleOP.Details, "浏览", 0)]
    [DataHandler(ModuleOP.Create, "新增", 1)]
    [DataHandler(ModuleOP.Edit, "修改", 2)]
    [DataHandler(ModuleOP.Delete, "删除", 3)]
    [DataHandler(ModuleOP.Refresh, "刷新", 4)]
    [DataHandler("Roles", "角色", 4)]
    [DataHandler("File", "档案", 5) ]
    public partial class UserIndexViewModel : ExtSharpList
    {
        public UserIndexViewModel()
        {
            this.Feature = ListFeature.MultiSelect | ListFeature.Paging | ListFeature.RowNumber;
            this.IsUseOrgTreeLayout = true;
            this.ToolbarButtonParameters.Add(new ToolButtonInfo()
            {
                Mode = "Roles",
                Direct = new ToolButtonInfo.DirectClickAction("LoadWindowForUser", "Role"),
                Parameters = new List<ToolButtonInfo.Parameter>()
                {
                    new ToolButtonInfo.Parameter("userId",
                        "this.up('grid').getSelectionModel().getSelection()[0].getData().Id", (int) ParameterMode.Raw)
                }
            });
            this.ToolbarButtonParameters.Add(new ToolButtonInfo()
            {
                Mode = "File",
                Direct = new ToolButtonInfo.DirectClickAction("Edit", "Employee"),
                Parameters = new List<ToolButtonInfo.Parameter>()
                {
                    new ToolButtonInfo.Parameter("id",
                        "this.up('grid').getSelectionModel().getSelection()[0].getData().Id", (int) ParameterMode.Raw)
                }
            });
        }
        /// <summary>
        /// 自定义查询面板
        /// </summary>
        protected override void InitQueryPanel()
        {
            base.InitQueryPanel();
            var helper = new SharpListHelper<UserIndexViewModel>(this);
            helper.AddQueryItem(x => x.Roles).Text("角色")
                .SearchFor("Roles.Id").ValueField("Id").DisplayField("Name");
        }

        public int Id { get; set; }
        [Column(Text = "用户名", Width = 200), QueryOption(Option = QueryFeature.DefualtQuery)]
        public string UserName { get; set; }

        [Column(Text = "电子邮件", Width = 200)]
        public string Email { get; set; }


        [DateColumn(Text = "注册日期", Width = 144, Format = "yyyy-MM-dd", Align = Alignment.Right)]
        public DateTime? DateRegistered { get; set; }

        [Column(Text = "最后登录IP", Width = 200)]
        public string LastLoginIP { get; set; }

        [DateColumn(Text = "最后登录日期", Width = 140, Format = "yyyy-MM-dd HH:mm:ss", Align = Alignment.Right)]
        public DateTime? DateLastLogon { get; set; }

        /// <summary>
        ///     是否被锁定
        /// </summary>
        [BooleanColumn(Text = "是否被锁定", TrueText = "锁定", FalseText = "正常", Align = Alignment.Center)]
        public bool IsLockedOut { get; set; }


        /// <summary>
        /// 角色
        /// </summary>
        [CustomField(Sharp.Portal.Mvc.Form.FieldType.DropDownList, "Name", "Id", nameof(Roles))]
        public IEnumerable<RoleDTO> Roles { get; set; }
    }
}